/*
 *    Copyright (c) 2023 李伟国
 *    moyu is licensed under Mulan PSL v2.
 *    You can use this software according to the terms and conditions of the Mulan PSL v2.
 *    You may obtain a copy of Mulan PSL v2 at:
 *             http://license.coscl.org.cn/MulanPSL2
 *    THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 *    See the Mulan PSL v2 for more details.
 */

package cn.moyu.kettle;

import cn.moyu.conf.properties.MoyuKettleInitProperties;
import lombok.extern.slf4j.Slf4j;
import org.pentaho.di.core.KettleEnvironment;
import org.pentaho.di.core.exception.KettleException;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * kettle初始化
 */

@Slf4j
@Component
public class KettleInit implements ApplicationRunner {

    @Resource
    private MoyuKettleInitProperties kettleInitProperties;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        initKettle();
    }

    /**
     * 初始化kettle
     */
    public void initKettle(){
        // 配置Kettle的JNDI的根路径
        System.getProperties().put("KETTLE_JNDI_ROOT",kettleInitProperties.getJndiRoot());
        // 配置Kettle的加目录
        System.getProperties().put("KETTLE_HOME",kettleInitProperties.getWorkHome());
        // 初始化Kettle环境
        try {
            log.info("Kettle开始初始化");
            KettleEnvironment.init();
            log.info("Kettle初始化完成");
        } catch (KettleException e) {
            log.error("kettle初始化失败\n{}", e.getLocalizedMessage());
        }
    }
}
